<template>
  <div class="model-training-container">
    <h1 class="page-title">气象预测模型训练平台</h1>
    
    <div class="model-cards-container">
      <!-- LSTM模型卡片 -->
      <div class="model-card" :class="{ 'training': lstmTraining }">
        <div class="model-header">
          <h2>LSTM时间序列模型</h2>
          <div class="model-status">
            <span v-if="lstmTraining" class="status-badge training">训练中...</span>
            <span v-else-if="lstmSuccess" class="status-badge success">训练完成</span>
            <span v-else class="status-badge idle">待训练</span>
          </div>
        </div>
        
        <div class="model-params">
          <div class="param-item">
            <label for="lstm-target">目标变量:</label>
            <select id="lstm-target" v-model="lstmParams.target">
              <option value="guilin_temp">桂林温度</option>
            </select>
          </div>
          
          <div class="param-row">
            <div class="param-item">
              <label for="lstm-epochs">训练轮次:</label>
              <input id="lstm-epochs" type="number" v-model.number="lstmParams.epochs" min="10" max="500">
            </div>
            <div class="param-item">
              <label for="lstm-batch-size">批次大小:</label>
              <input id="lstm-batch-size" type="number" v-model.number="lstmParams.batch_size" min="16" max="256">
            </div>
          </div>
          
          <div class="param-row">
            <div class="param-item">
              <label for="lstm-look-back">回溯天数:</label>
              <input id="lstm-look-back" type="number" v-model.number="lstmParams.look_back" min="30" max="365">
            </div>
            <div class="param-item">
              <label for="lstm-forecast">预测天数:</label>
              <input id="lstm-forecast" type="number" v-model.number="lstmParams.forecast_horizon" min="7" max="180">
            </div>
          </div>
          
          <div class="param-item">
            <label for="lstm-test-size">测试集比例:</label>
            <input id="lstm-test-size" type="range" v-model.number="lstmParams.test_size" min="0.1" max="0.5" step="0.05">
            <span>{{ (lstmParams.test_size * 100).toFixed(0) }}%</span>
          </div>
        </div>
        
        <button class="train-btn" @click="trainLSTM" :disabled="lstmTraining">
          {{ lstmTraining ? '训练中...' : '训练LSTM模型' }}
        </button>
        
        <div v-if="lstmSuccess" class="success-message">
          <i class="fas fa-check-circle"></i>
          LSTM模型训练成功
        </div>
      </div>
      
      <!-- 集成学习模型卡片 -->
      <div class="model-card" :class="{ 'training': ensembleTraining }">
        <div class="model-header">
          <h2>集成学习模型</h2>
          <div class="model-status">
            <span v-if="ensembleTraining" class="status-badge training">训练中...</span>
            <span v-else-if="ensembleSuccess" class="status-badge success">训练完成</span>
            <span v-else class="status-badge idle">待训练</span>
          </div>
        </div>
        
        <div class="model-params">
          <div class="param-item">
            <label for="ensemble-target">目标变量:</label>
            <select id="ensemble-target" v-model="ensembleParams.target_col">
              <option value="guilin_max">桂林最高温</option>
              <option value="guilin_min">桂林最低温</option>
            </select>
          </div>
          
          <div class="param-item">
            <label for="ensemble-horizon">预测天数:</label>
            <input id="ensemble-horizon" type="number" v-model.number="ensembleParams.horizon" min="1" max="180">
          </div>
        </div>
        
        <button class="train-btn" @click="trainEnsemble" :disabled="ensembleTraining">
          {{ ensembleTraining ? '训练中...' : '训练集成模型' }}
        </button>
        
        <div v-if="ensembleSuccess" class="success-message">
          <i class="fas fa-check-circle"></i>
          集成学习模型训练成功
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue';
import { trainLSTMModel } from '@/api/trainLSTMAPI';
import type { LSTMTrainParams, LSTMTrainResponse } from '@/api/trainLSTMAPI';
import { trainEnsembleModel } from '@/api/trainEnsembleAPI';
import type { EnsembleTrainParams, EnsembleTrainResponse } from '@/api/trainEnsembleAPI';
// LSTM模型状态和数据
const lstmParams = reactive<LSTMTrainParams>({
  target: 'guilin_temp',
  test_size: 0.2,
  epochs: 100,
  batch_size: 64,
  look_back: 180,
  forecast_horizon: 90
});

const lstmTraining = ref(false);
const lstmSuccess = ref(false);

// 集成学习模型状态和数据
const ensembleParams = reactive<EnsembleTrainParams>({
  target_col: 'guilin_max',
  horizon: 7
});

const ensembleTraining = ref(false);
const ensembleSuccess = ref(false);

// 训练LSTM模型
const trainLSTM = async () => {
  lstmTraining.value = true;
  lstmSuccess.value = false;
  
  try {
    const result = await trainLSTMModel(lstmParams);
    if (result.status === 'success') {
      lstmSuccess.value = true;
    } else {
      console.error('LSTM训练失败:', result.message);
    }
  } catch (error) {
    console.error('LSTM训练请求失败:', error);
  } finally {
    lstmTraining.value = false;
  }
};

// 训练集成学习模型
const trainEnsemble = async () => {
  ensembleTraining.value = true;
  ensembleSuccess.value = false;
  
  try {
    const result = await trainEnsembleModel(ensembleParams);
    if (result.status === 'success') {
      ensembleSuccess.value = true;
    } else {
      console.error('集成模型训练失败:', result.message);
    }
  } catch (error) {
    console.error('集成模型训练请求失败:', error);
  } finally {
    ensembleTraining.value = false;
  }
};
</script>

<style scoped>
/* 新增全局样式 */
html, body {
  height: 100%;
  margin: 0;
  padding: 0;
  overflow: hidden;
}

/* 基础样式 */
.model-training-container {
  width: 100%;
  height: 100vh;
  margin: 0;
  padding: 2rem;
  font-family: 'Roboto', sans-serif;
  background-color: #f5f7fa;
  border-radius: 0;
  overflow: auto; /* 只在容器内滚动 */
  box-sizing: border-box;
}

.page-title {
  text-align: center;
  color: #2c3e50;
  margin-bottom: 2rem;
  font-weight: 600;
  font-size: 2rem;
  padding-bottom: 1rem;
  border-bottom: 1px solid #eaecef;
}

.model-cards-container {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(500px, 1fr));
  gap: 2rem;
  max-width: 1400px;
  margin: 0 auto;
  box-sizing: border-box;
}

@media (max-width: 600px) {
  .model-cards-container {
    grid-template-columns: 1fr;
  }
  .model-training-container {
    padding: 1rem;
  }
}

/* 模型卡片样式 */
.model-card {
  background: white;
  border-radius: 12px;
  padding: 2rem;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.08);
  transition: all 0.3s ease;
  border: 1px solid #e1e4e8;
  height: 100%;
}

/* 确保卡片内容不会导致容器溢出 */
.model-card {
  max-height: calc(100vh - 10rem);
  overflow-y: auto;
}

.model-card.training {
  border-left: 4px solid #3498db;
  box-shadow: 0 4px 12px rgba(52, 152, 219, 0.2);
}

.model-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 15px rgba(0, 0, 0, 0.1);
}

.model-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1.5rem;
  padding-bottom: 1rem;
  border-bottom: 1px solid #eee;
}

.model-header h2 {
  margin: 0;
  color: #34495e;
  font-size: 1.4rem;
}

.status-badge {
  padding: 0.3rem 0.8rem;
  border-radius: 20px;
  font-size: 0.8rem;
  font-weight: 500;
}

.status-badge.idle {
  background: #f1f1f1;
  color: #7f8c8d;
}

.status-badge.training {
  background: #e3f2fd;
  color: #1976d2;
}

.status-badge.success {
  background: #e8f5e9;
  color: #388e3c;
}

/* 参数设置样式 */
.model-params {
  margin-bottom: 1.5rem;
}

.param-row {
  display: flex;
  gap: 1rem;
  margin-bottom: 1rem;
}

.param-item {
  flex: 1;
  margin-bottom: 1rem;
}

.param-item label {
  display: block;
  margin-bottom: 0.5rem;
  font-size: 0.9rem;
  color: #555;
  font-weight: 500;
}

.param-item input[type="number"],
.param-item select {
  width: 100%;
  padding: 0.6rem 0.8rem;
  border: 1px solid #ddd;
  border-radius: 6px;
  font-size: 0.9rem;
  transition: border 0.3s;
}

.param-item input[type="number"]:focus,
.param-item select:focus {
  border-color: #3498db;
  outline: none;
  box-shadow: 0 0 0 2px rgba(52, 152, 219, 0.2);
}

.param-item input[type="range"] {
  width: calc(100% - 3rem);
  margin-right: 0.5rem;
  vertical-align: middle;
}

/* 按钮样式 */
.train-btn {
  width: 100%;
  padding: 0.8rem;
  background: linear-gradient(135deg, #3498db, #2980b9);
  color: white;
  border: none;
  border-radius: 6px;
  font-size: 1rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s;
  margin-bottom: 1.5rem;
}

.train-btn:hover {
  background: linear-gradient(135deg, #2980b9, #3498db);
  transform: translateY(-1px);
  box-shadow: 0 4px 8px rgba(41, 128, 185, 0.3);
}

.train-btn:disabled {
  background: #bdc3c7;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

/* 成功信息样式 */
.success-message {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  color: #388e3c;
  font-weight: 500;
  padding: 0.8rem;
  background-color: #f0fff4;
  border-radius: 6px;
  margin-top: 1rem;
}

/* 页面背景增强 */
body {
  background-color: #f0f2f5;
  margin: 0;
}
</style>